//
//  BaseTabBarViewController.m
//  bus
//
//  Created by ygs on 15/6/6.
//  Copyright (c) 2015年 ygs. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "HomeViewController.h"
#import "LifeViewController.h"
#import "LazyCartViewController.h"
#import "MyViewController.h"
#import "WholeSaleSubVC.h"
#define PressTextColor SetColor(13,153,68,1)
#define NomalTextColor SetColor(100,100,100,1)
@interface BaseTabBarViewController ()
{
    LazyCartViewController *lazyVC;
    NSInteger oldIndex;
}
@end

@implementation BaseTabBarViewController
-(void)viewWillAppear:(BOOL)animated
{
    [self.selectedViewController beginAppearanceTransition: YES animated: animated];
}

-(void) viewDidAppear:(BOOL)animated
{
    [self.selectedViewController endAppearanceTransition];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [self.selectedViewController beginAppearanceTransition: NO animated: animated];
}

-(void) viewDidDisappear:(BOOL)animated
{
    [self.selectedViewController endAppearanceTransition];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    imgNomalAry =[[NSArray alloc] initWithObjects:@"shouye_new.png",@"pifa_new2.png",@"wode_new.png",nil];
    imgPressAry =[[NSArray alloc] initWithObjects:@"shouye_cur_new.png",@"pifa_cur_new",@"wode_cur_new.png",nil];
    
//    imgNomalAry =[[NSArray alloc] initWithObjects:@"shouye_new.png",@"wode_new.png",nil];
//    imgPressAry =[[NSArray alloc] initWithObjects:@"shouye_cur_new.png",@"wode_cur_new.png",nil];
    
    [self initViewController];
    [self initTabBar];  // 创建tabBar
    self.tabBar.hidden = YES;
    
    [AppData share].baseTabVC = self;
}
-(void)initTabBar
{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight-50, ScreenWidth, 50)];
    view.backgroundColor=[UIColor whiteColor];
    self.tabView=view;
    self.title=@"";
    [self.view addSubview:_tabView];
    
    
    int btnSize=ScreenWidth/imgNomalAry.count;
    for (int i=0; i<imgNomalAry.count; i++)
    {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(i*btnSize+(btnSize - 40)/2, (50-36)/2, 40, 36);
        btn.tag=i+20;
        if (i == 0) {
            oldIndex = 0;
            [btn setBackgroundImage:[UIImage imageNamed:[imgPressAry objectAtIndex:i]] forState:UIControlStateNormal];
        }else{
            [btn setBackgroundImage:[UIImage imageNamed:[imgNomalAry objectAtIndex:i]] forState:UIControlStateNormal];
        }
        btn.backgroundColor = [UIColor whiteColor];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        btn.adjustsImageWhenHighlighted = NO;
        [btn setHitTestEdgeInsets:UIEdgeInsetsMake(0, -(btnSize - 40)/2, 0, -(btnSize - 40)/2)];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_tabView addSubview:btn];
        
        
//        
//        if (i == 2) {
//            //购物车 数量
//            _countLable = [[UILabel alloc] initWithFrame:CGRectMake(btn.right-20, 0, 20, 20)];
//            _countLable.backgroundColor = [UIColor redColor];
//            _countLable.font = FontSet(12);
//            _countLable.layer.cornerRadius = 10;
//            _countLable.hidden = YES;
//            _countLable.textAlignment = NSTextAlignmentCenter;
//            _countLable.text = @"0";
//            _countLable.textColor = [UIColor whiteColor];
//            _countLable.layer.masksToBounds = YES;
//            [_tabView addSubview:_countLable];
//            
//        }else
            if (i == 1) {
            //个人中心小红点           CGRectMake(btn.right-10, 5, 6, 6)
//            _redLable = [[UILabel alloc] initWithFrame:CGRectMake(btn.right-14, 8, 6, 6)];
//            _redLable.backgroundColor = [UIColor redColor];
//            _redLable.layer.cornerRadius = 3;
//            _redLable.layer.masksToBounds = YES;
//            _redLable.hidden = YES;
//            [_tabView addSubview:_redLable];
        }
    }
    
    UILabel *lineLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    lineLable.backgroundColor = SetColor(0, 0, 0, 0.1);
    [view addSubview:lineLable];
}
#pragma mark - tabBar跳转
- (void)initViewController{
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    lazyVC = [[LazyCartViewController alloc] init];
    MyViewController *myVC = [[MyViewController alloc] init];
//    LifeViewController *life = [[LifeViewController alloc] init];
    WholeSaleSubVC *wholeSaleVC = [[WholeSaleSubVC alloc]init];
    wholeSaleVC.titleStr = @"批发进货";
    wholeSaleVC.isPiFa = NO;//判断是不是批发账户，用来区分界面显示
    
    NSArray *controllerArr = @[myVC,wholeSaleVC,lazyVC];
    NSMutableArray *mArr = [[NSMutableArray alloc] init];
    for (int i =0; i<controllerArr.count; i++) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[controllerArr objectAtIndex:i]];
        nav.delegate = self;
        nav.navigationBarHidden = YES;
        [mArr addObject:nav];
    }
    
    self.viewControllers = mArr;

}
- (void)btnClick:(UIButton *)btn{
    [self changeBottomBtnWithIndex:btn.tag-20];
}
- (void)changeBottomBtnWithIndex:(NSInteger)index{
    for (int i=0; i<imgPressAry.count; i++) {
        UIButton *tabBtn = (UIButton *)[_tabView viewWithTag:20+i];
        [tabBtn setBackgroundImage:[UIImage imageNamed:[imgNomalAry objectAtIndex:i]] forState:UIControlStateNormal];
        
        UILabel *lable = (UILabel *)[_tabView viewWithTag:30+i];
        lable.textColor = NomalTextColor;
    }
    UIButton *btn = (UIButton*)[_tabView viewWithTag:20+index];
    [btn setBackgroundImage:[UIImage imageNamed:[imgPressAry objectAtIndex:index]] forState:UIControlStateNormal];
    
    UILabel *lable = (UILabel *)[_tabView viewWithTag:30+index];
    lable.textColor = PressTextColor;
    
    self.selectedIndex = index;
    
    if (index != oldIndex) {
        if (index == 2) {
            //点击购物车 判断是否登陆
        }else if (index == 1) {
//            [lazyVC refresh];
        }
    }
    oldIndex = index;
}
 
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    NSInteger coumt=navigationController.viewControllers.count;
    if (coumt>=2)
    {
        
        [UIView animateWithDuration:.15 animations:^{
            _tabView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 50);
        } completion:^(BOOL finished) {
            _tabView.hidden=YES;
        }];
    }else {
        _tabView.hidden=NO;
        
        [UIView animateWithDuration:.15 animations:^{
            _tabView.frame = CGRectMake(0, ScreenHeight-50, ScreenWidth, 50);
        }];
    }
}

- (void)theShoppingCartCount:(NSString *)count {
    _countLable.text = [NSString stringWithFormat:@"%@",count];
    if ([count intValue] <= 0) {
        _countLable.hidden = YES;
    }else {
        _countLable.hidden = NO;
    }
}
#pragma mark - 
- (void)goLoginView {
    LoginViewController *login = [[LoginViewController alloc] init];
    [self presentViewController:login animated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
